package com.lizard.schedule.persistence.db.table;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lizard.schedule.persistence.db.DbHelper;
import com.lizard.schedule.persistence.db.bean.DownloadThreadInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 正在下载的线程信息表
 */
public class DownloadThreadInfoTbl {

    private static DownloadThreadInfoTbl instance;

    /**
     * 表名
     */
    private final String TBL_NAME = "d_tbl";

    /**
     * 表字段：id
     */
    private final String FIELD_ID = "_id";

    /**
     * 表字段：下载文件信息表id
     */
    private final String FIELD_DOWNLOAD_FILE_INFO_ID = "f_a";

    /**
     * 表字段：http Range开始位置
     */
    private final String FIELD_START = "f_b";

    /**
     * 表字段：http Range结束位置
     */
    private final String FIELD_END = "f_c";

    /**
     * 表字段：已下载的长度
     */
    private final String FIELD_LENGTH = "f_d";

    private DownloadThreadInfoTbl(){}

    public static DownloadThreadInfoTbl getInstance(){
        if(instance == null){
            synchronized (DownloadThreadInfoTbl.class){
                if(instance == null){
                    instance = new DownloadThreadInfoTbl();
                }
            }
        }
        return instance;
    }

    /**
     * 搜索下载线程列表
     *
     * @param downloadFileInfoId
     * @return
     */
    public List<DownloadThreadInfo> select(int downloadFileInfoId){
        List<DownloadThreadInfo> downloadThreadInfos = new ArrayList<DownloadThreadInfo>();
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("SELECT * FROM ").append(TBL_NAME).append(" WHERE ")
                .append(FIELD_DOWNLOAD_FILE_INFO_ID).append(" = ?;");
        Cursor cursor = DbHelper.getInstance().getSqliteDatabase().rawQuery(sqlBuilder.toString(),
                new String[]{downloadFileInfoId + ""});
        while(cursor.moveToNext()){
            DownloadThreadInfo downloadThreadInfo = new DownloadThreadInfo();
            downloadThreadInfo.set_id(cursor.getInt(cursor.getColumnIndex(FIELD_ID)));
            downloadThreadInfo.setDownloadFileInfoId(cursor.getInt(cursor.getColumnIndex(FIELD_DOWNLOAD_FILE_INFO_ID)));
            downloadThreadInfo.setStart(cursor.getInt(cursor.getColumnIndex(FIELD_START)));
            downloadThreadInfo.setEnd(cursor.getInt(cursor.getColumnIndex(FIELD_END)));
            downloadThreadInfo.setLength(cursor.getInt(cursor.getColumnIndex(FIELD_LENGTH)));
            downloadThreadInfos.add(downloadThreadInfo);
        }
        cursor.close();
        return downloadThreadInfos;
    }

    /**
     * 更新数据
     * @param downloadThreadInfo
     */
    public void update(DownloadThreadInfo downloadThreadInfo){
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("UPDATE ").append(TBL_NAME).append(" SET ")
                .append(FIELD_DOWNLOAD_FILE_INFO_ID).append(" = ?,")
                .append(FIELD_START).append(" = ?,")
                .append(FIELD_END).append(" = ?,")
                .append(FIELD_LENGTH).append(" = ?")
                .append(" WHERE ").append(FIELD_ID).append(" = ?;");
        DbHelper.getInstance().getSqliteDatabase().execSQL(sqlBuilder.toString(),
                new Object[]{downloadThreadInfo.getDownloadFileInfoId(),
                        downloadThreadInfo.getStart(),downloadThreadInfo.getEnd(),
                        downloadThreadInfo.getLength(), downloadThreadInfo.get_id()});
    }

    /**
     * 删除数据
     *
     * @param downloadFileInfoId
     */
    public void delete(int downloadFileInfoId){
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("DELETE FROM ").append(TBL_NAME)
                .append(" WHERE ").append(FIELD_DOWNLOAD_FILE_INFO_ID).append(" = ?;");
        DbHelper.getInstance().getSqliteDatabase().execSQL(sqlBuilder.toString(),
                new Object[]{downloadFileInfoId});
    }

    /**
     * 插入数据
     *
     * @param downloadThreadInfo
     */
    public void insert(DownloadThreadInfo downloadThreadInfo){
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("INSERT INTO ").append(TBL_NAME).append("(")
                .append(FIELD_DOWNLOAD_FILE_INFO_ID).append(",")
                .append(FIELD_START).append(",")
                .append(FIELD_END).append(",")
                .append(FIELD_LENGTH).append(") VALUES(?, ?, ?, ?);");
        DbHelper.getInstance().getSqliteDatabase().execSQL(sqlBuilder.toString(),
                new Object[]{downloadThreadInfo.getDownloadFileInfoId(), downloadThreadInfo.getStart(),
                downloadThreadInfo.getEnd(), downloadThreadInfo.getLength()});
    }

    /**
     * 创建表
     *
     * @param db
     */
    public void createTbl(SQLiteDatabase db){
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("CREATE TABLE IF NOT EXISTS ").append(TBL_NAME).append("(")
                .append(FIELD_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT,")
                .append(FIELD_DOWNLOAD_FILE_INFO_ID).append(" INTEGER,")
                .append(FIELD_START).append(" INTEGER,")
                .append(FIELD_END).append(" INTEGER,")
                .append(FIELD_LENGTH)
                .append(");");
        db.execSQL(sqlBuilder.toString());
    }

    /**
     * 删除表
     *
     * @param db
     */
    public void dropTbl(SQLiteDatabase db){
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("DROP TABLE IF EXISTS ").append(TBL_NAME);
        db.execSQL(sqlBuilder.toString());
    }

}
